import { Filds } from '@/common/type';
import { queryRiskTrend, queryRiskStatistic, queryRiskHotList, queryPage, queryInfoById, create, update } from './service';
import { queryDictList } from '@/services/dict';
import { routerRedux } from 'dva/router';
import { DICT } from './enum';
import { message } from 'antd';

export default {
  namespace: 'analyse_risk',

  state: {
    list: { data: [] },
    details: [],
    cond: {},
    dict: {},
    hotRiskList: { data: [] }, //隐患高发部位top10
    riskStatistic: {}, //隐患统计-隐患数
    riskTrend: {},  //隐患趋势
  },

  effects: {
    *queryRiskTrend({ payload }, { call, put }) {
      const response = yield call(queryRiskTrend, { type: payload.type })
      yield put({ type: 'setRiskTrend', payload: response })
    },

    //隐患统计-隐患数
    *queryRiskStatistic(_, { call, put }) {
      const data = yield call(queryRiskStatistic)
      yield put({ type: 'setRiskStatistic', payload: data })
    },

    //隐患高发部位top10
    *queryRiskHotList(_, { call, put }) {
      const data = yield call(queryRiskHotList)
      yield put({ type: 'setRiskHotList', payload: data })
    },
    *initCond(_, { put }) {
      yield put({ type: 'setCond', payload: {} });
      yield put({ type: 'queryPage' })
    },
    *updateCond({ payload }, { put }) {
      yield put({ type: 'setCond', payload });
      yield put({ type: 'queryPage' })
    },
    *queryPage({ payload = { pageNum: 1, pageSize: 10 } }, { call, put, select }) {
      const cond = yield select(state => state.analyse_risk.cond);
      const params = { ...payload, ...cond };
      if (!params.pageSize) params.pageSize = 10;
      const { list = [], total } = yield call(queryPage, params);
      yield put({ type: 'queryDict', payload: { keyList: DICT } });
      yield put({ type: 'setPage', payload: { list, total: +total } })
    },
    *queryDict({ payload }, { call, put }) {
      const dict = yield call(queryDictList, payload);
      yield put({ type: 'setDict', payload: dict })
    },
    *queryInfoById({ payload }, { call, put }) {
      const response = yield call(queryInfoById, payload);
      yield put({ type: 'setInfo', payload: response })
    },
    *queryEditInfoById({ payload }, { call, put }) {
      const response = yield call(queryInfoById, payload.id);
      const items: Filds = {};
      payload.keys.forEach((item: string) => {
        items[item] = response[item];
      });
      yield put({ type: 'setEditInfo', payload: items })
    },
    *create({ payload }, { call, put }) {
      const ans = yield call(create, payload);
      if (ans) {
        message.success(ans);
        yield put(routerRedux.push('/data/analyse_risk'));
      }
    },
    *update({ payload }, { call, put }) {
      yield call(update, payload);
      yield put(routerRedux.push('/data/analyse_risk'));
    },
  },

  reducers: {
    //隐患统计-隐患数
    setRiskStatistic(state, action) {
      return {
        ...state,
        riskStatistic: action.payload
      }
    },
    
    //隐患高发部位top10
    setRiskHotList(state, action) {

      return {
        ...state,
        hotRiskList: action.payload
      }
    },
    setCond(state, action) {
      return {
        ...state,
        cond: action.payload
      }
    },
    setPage(state, action) {
      return {
        ...state,
        list: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    setDict(state, action) {
      return {
        ...state,
        dict: action.payload
      }
    },
    setInfo(state, action) {
      return {
        ...state,
        details: action.payload
      }
    },
    setEditInfo(state, action) {
      return {
        ...state,
        editDetails: action.payload
      }
    },
    setRiskTrend(state, action) {
      return {
        ...state,
        riskTrend: action.payload
      }
    },
  }
}
